#include <iostream>

using namespace std;

typedef long long LL;

const int N = 2e5 + 10;
int pre[N], sur[N];
int a[N], b[N];
int n, m;


int main()
{
	int T; cin >> T;
	while(T--)
	{
		cin >> n >> m;
		for(int i = 1; i <= n; i++) cin >> a[i];
		for(int j = 1; j <= m; j++) cin >> b[j];
		int j = n;
		for(int i = m; i >= 1; i--)
		{
			while(j >= 1 && a[j] < b[i]) j--;
			sur[i] = j--;
		} 
		j = 1;
		for(int i = 1; i <= m; i++)
		{
			while(j <= n && a[j] < b[i]) j++;
			pre[i] = j++;
		}
		if(pre[m] <= n)
		{
			cout << 0 << endl;
			continue;
		}
		int ans = 0x3f3f3f3f;
		for(int i = 1; i <= m; i++)
		{
			int pre_match = i == 1 ? 0 : pre[i - 1];
			int sur_match = i == m ? n + 1 : sur[i + 1];
			if(sur_match > pre_match) ans = min(ans, b[i]); 
		}
		if(ans == 0x3f3f3f3f) cout << -1 << endl;
		else cout << ans << endl;
	
	}
	return 0;	
} 
